<template>
	<view>
		<mc-framework navTitle="MES工控机" @changeLevel="changeLevel">
			<template #workbench>
				<view class="TabsBox">

				</view>
			</template>

			<!-- 主内容 -->
			<template #content>
				<view class="content_box f-dc">
					<view class="table table_top_th">
						<table>
							<tr>
								<th width='30%'>故障单号</th>
								<th width='25%'>故障模具</th>
								<th width='25%'>故障日期</th>
								<th width='10%'>申报人</th>
								<th width='10%'>处理人</th>
							</tr>
						</table>
					</view>
					<z-paging ref="paging" v-model="contents" @query="queryList" :fixed="false" class="z_paging">
						<view class="table">
							<table>
								<tbody>
									<tr v-for="(item,index) in contents" @click="planIDClick(item,index)"
										:class="{ hovered: hoveredIndex === index }">
										<td width='30%'>
											<div class="ListTdDiv"><text>{{item.BillNo}}</text></div>
										</td>
										<td width='25%'>
											<div class="ListTdDiv"><text>{{item.MoludName}}</text></div>
										</td>
										<td width='25%'>
											<div class="ListTdDiv"><text>{{item.BillDate}}</text></div>
										</td>
										<td width='10%'>
											<div class="ListTdDiv"><text>{{item.Declarer}}</text></div>
										</td>
										<td width='10%'>
											<div class="ListTdDiv"><text>{{item.Notifier}}</text></div>
										</td>

									</tr>
								</tbody>
							</table>

						</view>

					</z-paging>

				</view>

				<view class="add">
					<!-- <u-icon name="scan" color="#fff" size="30" @click="scan"></u-icon> -->
					<u-icon name="plus" color="#fff" size="30" @click="addplus"></u-icon>
				</view>
				<view class="search">
					<u-icon name="search" color="#fff" size="30" @click="search"></u-icon>
				</view>

				<uni-popup ref="popup" type="bottom" background-color="#fff" border-radius="10px 10px 0 0"
					@change="changePopup">
					<view class="popup_content">

						<u-cell-group :border='false'>
							<u-cell title="故障模具" :isLink="false" :value='FormData.ProductName'
								@click="addBillNo"></u-cell>
							<u-cell title="开始时间" :isLink="false" :value='FormData.FBDate'
								@click="addTime('开始')"></u-cell>
							<u-cell title="结束时间" :isLink="false" :value='FormData.FEDate'
								@click="addTime('结束')"></u-cell>
						</u-cell-group>
						<view class="bottomBtns">
							<button block @click="submit">
								查 &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 询
							</button>
						</view>

					</view>
				</uni-popup>
				<u-datetime-picker :show="show" v-model="value1" mode="date" @cancel='show = false'
					@confirm='pickerConfirm'></u-datetime-picker>

				<u-action-sheet :actions="BillNoList" :show="BillNoShow" @select="BillNoListClick"
					@close="BillNoShow=false"></u-action-sheet>

			</template>

		</mc-framework>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime
	} from '@/mes/common/base.js';

	export default {
		data() {
			return {
				FormData: {
					PageIndex: 1,
					PageSize: 30,
					FBDate: '',
					FEDate: '',
					DeviceID: 0,
					DeviceIDName: 0,
					EquipmentName: '全部',
					BillType: 'MJYC',
					ProductID: 0,
					ProductName: '全部',
					disabled: false
				},
				contents: [],
				show: false,
				value1: Number(new Date()),
				TimeName: '',
				hoveredIndex: null,
				BillNoShow: false,
				BillNoList: [],
				disabled: false
			};
		},
		onLoad() {
			var that = this
			that.FormData.FBDate = getBefore7DateTime()
			that.FormData.FEDate = getFormatDateTime(new Date())
			this.$store.state.GuyueModule.MesBasicInfo.Status.ClockStatusTitle = '其他'

		},
		onShow() {

			this.clearHovered();
			this.getList
		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		mounted() {

		},
		methods: {
			getList() {
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();
			},
			BillNoListClick(e) {
				let that = this;
				this.BillNoShow = false
				console.log(e);
				that.FormData.ProductID = e.MoludID
				that.FormData.ProductName = e.MoludCode
				this.$forceUpdate()
			},
			addBillNo() {
				var that = this
				that.Request.request('Droplist_Molud', {
					MoludCode: 0
				}, (ret) => {
					if (ret.status == 200) {
						var obj = {
							MoludID: 0,
							MoludCode: "全部"
						}
						ret.data.ds.unshift(obj)
						ret.data.ds.map(item => item.name = item.MoludCode)
						that.BillNoList = ret.data.ds
						that.BillNoShow = true
					}
				})
			},
			addplus() {

				let that = this;
				var url
				let routes = getCurrentPages();
				let obj = {
					Name: '',
					APPFunID: '',
					length: routes.length + 1
				}
				var title = '模具异常'
				url = `./exceptionDetails?titleName=${title}`
				obj.Name = '故障详情'
				obj.APPFunID = '故障详情'
				that.GonavigateTo(obj, url)

			},
			planIDClick(item, index) {
				let that = this;
				this.hoveredIndex = index; // 设置当前hover的tr索引  

				var url
				let routes = getCurrentPages();
				let obj = {
					Name: '',
					APPFunID: '',
					length: routes.length + 1
				}
				item.title = '模具异常'
				url = `./exceptionDetails?item=${JSON.stringify(item)}`
				obj.Name = '故障详情'
				obj.APPFunID = '故障详情'
				that.GonavigateTo(obj, url)


			},
			GonavigateTo(obj, url) {
				let that = this;
				if (that.disabled == false) {

					that.disabled = true

					let newData = JSON.parse(JSON.stringify(this.$store.state.GuyueModule.ModuleData_select));
					newData.push(obj);
					this.$store.commit('GuyueModule/ChangeAndSetStateData', {
						Name1: 'ModuleData_select',
						data: newData
					})
					uni.navigateTo({
						url: url,
						animationType: 'slide-in-right', // 动画类型
						animationDuration: 300 // 动画时长
					});
					setTimeout(() => {
						that.disabled = false
					}, 4000)
				}
			},
			addTime(name) {
				this.TimeName = name
				if (this.TimeName == '开始') {
					this.value1 = Number(getTimeStamp(this.FormData.FBDate))
				} else {
					this.value1 = Number(getTimeStamp(this.FormData.FEDate))
				}
				this.show = true
			},
			search() {
				this.$refs.popup.open('bottom')
			},
			submit() {
				let that = this;
				this.$refs.popup.close('bottom')
				this.contents = []; // 假设这是异步获取数据的操作
				this.$refs.paging.reload();

			},
			pickerConfirm(e) {
				let that = this;
				var time = getFormatDateTime(e.value)
				if (this.TimeName == '开始') {
					this.FormData.FBDate = time
				} else {
					this.FormData.FEDate = time
				}
				this.show = false
			},
			changePopup() {

			},

			queryList(pageNo) {
				let that = this;
				try {
					this.FormData.PageIndex = pageNo
					that.Request.request('getFaultDocList', this.FormData, (ret) => {
						if (ret.status == 200) {
							ret.data.ds.map((item) => {
								item.BillDate = item.BillDate ? item.BillDate.replace('T', ' ') : ''
							})
							that.$refs.paging.complete(ret.data.ds);
							that.$forceUpdate()
						}
					})
				} catch (e) {
					console.log(e)
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}

			},

			changeLevel(data, row) {
				let routes = getCurrentPages(); //当前页面栈
				let backIndex = routes.length - row.length; // 计算返回页面栈数

				uni.navigateBack({
					delta: backIndex
				})
			},
			clearHovered() {
				this.hoveredIndex = null; // 清除hover的tr索引  
			},

		}
	}
</script>

<style lang="scss">
	@import "../css/index.css";
</style>